package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniuxy.entity.Payment;
import com.woniuxy.entity.Refund;
import com.woniuxy.entity.Registered;
import com.woniuxy.entity.Tde;
import com.woniuxy.mapper.PaymentMapper;
import com.woniuxy.mapper.RefundMapper;
import com.woniuxy.mapper.RegisteredMapper;
import com.woniuxy.mapper.TdeMapper;
import com.woniuxy.producer.DelayExchangeProducer;
import com.woniuxy.service.RegisteredService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.SnowflakeIdGenerator;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class RegisteredServiceImpl implements RegisteredService , SaasService {
    @Autowired
    TdeMapper tdeMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Autowired
    RegisteredMapper registeredMapper;
    @Autowired
    RefundMapper refundMapper;//退费
    @Autowired
    PaymentMapper paymentMapper;//缴费
    @Autowired
    DelayExchangeProducer delayExchangeProducer;
    @Override
    public ResponseData<?> queryDe() {
        List<Tde> tdes = (List<Tde>) redisTemplate.opsForHash().get("deList", "de");
        if(tdes!=null){
            return new ResponseData<>().ok(tdes);
        }
        List<Tde> tdeList = tdeMapper.queryList();
        redisTemplate.opsForHash().put("deList","de",tdeList);
        return new ResponseData<>().ok(tdeList);
    }

    @Override
    public ResponseData<?> queryReList(PageParam<Registered> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Registered> registereds = registeredMapper.queryList(pageParam.getParam());
        PageInfo pageInfo = new PageInfo(registereds);
        return new ResponseData<>().ok(pageInfo);

    }

    @Override
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<?> AddAndRefund(Registered registered) {
        Integer id = registered.getId();//挂号表id
        Integer rsCaseId = registered.getRsCaseId();//病人id

        SnowflakeIdGenerator snf = new SnowflakeIdGenerator();
        long BrNo = snf.nextId();
        Payment payment = paymentMapper.queryPayment(id);//根据挂号编号查到的缴费信息
        BigDecimal rfPrice = payment.getRfPrice();//缴费金额

        //增加一条退费表 修改挂号表的状态，修改缴费表的状态
        registeredMapper.updateRsStatus(2,id);
        paymentMapper.updateRfStatus(1,id);
        Refund refund = new Refund();
        refund.setBrNo(String.valueOf(BrNo));
        refund.setBrMoney(rfPrice);
        refund.setBrCaseId(rsCaseId);
        refund.setBrType("0");
        refundMapper.addBr(refund);

//        30分钟后修改挂号表的状态
//        Integer ttl= (Integer) (30*60*1000);
//        delayRegisterProducer.send(String.valueOf(id),"delayRs",10);

        return new ResponseData<>().ok();
    }


    @Override
    public ResponseData<?> queryRsNo(String rsNo) {
        Registered registered = registeredMapper.queryRsNo(rsNo);

        return new ResponseData<>().ok(registered);
    }
}
